package com.bite.rabbitmq.controller;

import com.bite.rabbitmq.constant.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/producer")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    @RequestMapping("/work")
    public String work(){
        // 使用内置交换机,交换机为"" , RountingKey 和队列名称一致
        for (int i = 0; i < 10; i++) {
            rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE,"hello spring amqp: work..."+i);

        }
        return "发送成功";
    }

    @RequestMapping("/fanout")
    public String fanout(){
        rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE,"","hello spring amqp: findout...");
        return "发送成功";
    }
    @RequestMapping("/direct/{rountingKey}")
    public String direct(@PathVariable("rountingKey") String rountingKey){
        rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE,rountingKey,"hello spring amqp: direct," +
                "my rountingKey is "+rountingKey);
        return "发送成功";
    }

    @RequestMapping("/topic/{rountingKey}")
    public String topic(@PathVariable("rountingKey") String rountingKey){
        rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE,rountingKey,"hello spring amqp: direct" +
                ",my rountingKey is "+rountingKey);
        return "发送成功";
    }
}
